dojo.require("dijit.dijit"); // optimize: load dijit layer
dojo.require("dijit.Dialog");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.RadioButton");
dojo.require("dijit.Tooltip");
dojo.require("dojo.parser"); // scan page for widgets

dojo.addOnLoad( function()
{
	// hide loading gif
	window.setTimeout(function()
	{
		document.getElementById('img').style.display='none';
		document.getElementById('form').style.visibility='';
	}, 100);
});

var check_corrent_input = function()
{
	if (
		dijit.byId('form_givenName').attr('value') == ''
		|| dijit.byId('form_lastName').attr('value') == ''
		|| dijit.byId('form_default_department').attr('value') == ''
	) {
		return false;
	}
	return true;
};

var submit_btn = function()
{
	if (!check_corrent_input())
	{
		dialog_error.show();
		return;
	}
	dialog_wait.show();
	submit_data();
};

var submit_data = function()
{
	dojo.xhrPost({
		url: "html?action=reg&mdl=register",
		content: {
			'gn': dijit.byId('form_givenName').attr('value'),
			'ln': dijit.byId('form_lastName').attr('value'),
			'dep': dijit.byId('form_default_department').attr('value'),
			'lang':	dojo.byId('form_lang').value
		},
	    handleAs: "text",
	    // timeout: 30000, infinity
	    load: function(rdata, ioArgs) {
			dialog_wait.hide();
			if (rdata != 'null')
			{
				alert ("Error while saving data. Please try again later. ("+rdata+")");
			} else
			{
				// you are registered
				dojo.byId('s_name').innerHTML = dijit.byId('form_givenName').attr('value') + ' ' + dijit.byId('form_lastName').attr('value');
				dojo.byId('form').style.display = 'none';
				dojo.byId('finished').style.display = 'block';
				window.opener.location.reload();
			}
	        return rdata;
	    },
	    error: function(response, ioArgs) {
	    	alert ("Error while saving data. Please try again later. ("+arguments[0]+")");
	    	dialog_wait.hide();
	        return response;
	    }
	});
};

// hack for another tooltip position
dijit.showTooltip = function(/*String*/ innerHTML, /*DomNode*/ aroundNode, /*String[]?*/ position){
	// summary:
	//		Display tooltip w/specified contents in specified position.
	//		See description of dijit.Tooltip.defaultPosition for details on position parameter.
	//		If position is not specified then dijit.Tooltip.defaultPosition is used.
	position = ["BR-TR"];
	if(!dijit._masterTT){ dijit._masterTT = new dijit._MasterTooltip(); }
	return dijit._masterTT.show(innerHTML, aroundNode, position);
};
